<?php 

defined('_JEXEC') or die('Restricted access');

JFormHelper::loadFieldClass('list');

class JFormFieldCategoryparent extends JFormFieldList {
	
	
	protected $type = 'Categoryparent';
	
	protected function getOptions() {
		
		$options = array();
		
		$db = JFactory::getDbo();
		$query = $db->getQuery(TRUE);
		
		$query->select($db->quoteName(array('id','name','parentid'),array('value','text','parentid')));
		$query->from($db->quoteName('#__doska_categories'));
		$query->where($db->quoteName('state'). ' = '.$db->quote(1));
		
		$db->setQuery($query);
		
		try {
			$row = $db->loadObjectList();
		}
		catch(RuntimeException $e) {
			JError::raiseWarning(500,$e->getMessage());
		}
		
		$parent = new stdClass;
		$parent->text = JText::_('JGLOBAL_ROOT_PARENT');
		$parent->value = 0;
		
		array_push($options,$parent);
		if($row) {
			for($i = 0; $i < count($row);$i++) {
				if($row[$i]->parentid == 0) {
					array_push($options,$row[$i]);
				}
			}
		}
		
		
		return $options;
		
	}
}
 